package com.owbtgad.common

import android.app.Application
import com.xj.anchortask.library.AnchorProject
import com.xj.anchortask.library.OnProjectExecuteListener
import com.xj.anchortask.library.log.LogUtils
import com.xj.anchortask.library.monitor.OnGetMonitorRecordCallback

/**
 * Created by jun xu on 2/9/21.
 */
object TestTaskUtils {

    fun executeTask(
        application: Application,
        projectExecuteListener: OnProjectExecuteListener? = null,
        onGetMonitorRecordCallback: OnGetMonitorRecordCallback? = null
    ) {
        val project =
            AnchorProject.Builder().setContext(application).setLogLevel(LogUtils.LogLevel.DEBUG)
                .setAnchorTaskCreator(ApplicationAnchorTaskCreator(application))
                .addTask(TASK_NAME_ZERO)
                .addTask(TASK_NAME_ONE).afterTask(TASK_NAME_ZERO)
                .addTask(TASK_NAME_TWO).afterTask(TASK_NAME_ZERO)
                /*.addTask(TASK_NAME_TWO)
                .addTask(TASK_NAME_THREE).afterTask(TASK_NAME_ZERO, TASK_NAME_ONE)
                .addTask(TASK_NAME_FOUR).afterTask(TASK_NAME_ONE, TASK_NAME_TWO)
                .addTask(TASK_NAME_FIVE).afterTask(TASK_NAME_THREE, TASK_NAME_FOUR)*/
                .build()
        project.addListener(object : OnProjectExecuteListener {
            override fun onProjectStart() {

            }

            override fun onTaskFinish(taskName: String) {

            }

            override fun onProjectFinish() {

            }

        })
        projectExecuteListener?.let {
            project.addListener(it)
        }
        project.onGetMonitorRecordCallback = object : OnGetMonitorRecordCallback {
            override fun onGetTaskExecuteRecord(result: Map<String?, Long?>?) {
                onGetMonitorRecordCallback?.onGetTaskExecuteRecord(result)
            }

            override fun onGetProjectExecuteTime(costTime: Long) {
                onGetMonitorRecordCallback?.onGetProjectExecuteTime(costTime)
            }

        }
        project.start()
            .await()
    }
}